<template>
  <div class="divBox">
    <el-card class="box-card">
      <el-form inline size="small" @submit.native.prevent>
        <el-form-item class="el-form-item-align-right">
          <el-button icon="el-icon-circle-plus" size="mini" type="primary" @click="handlerOpenEdit(0)"
                     v-has-permi="['admin:banner:save']">新增轮播图
          </el-button>
        </el-form-item>
      </el-form>
      <el-table :data="listData.list" class="my-table" size="mini"
                :header-cell-style=" {fontWeight:'bold',background:'#f8f8f9',color: '#515a6e',height:'40px'}" border>
        <el-table-column label="ID" prop="id" width="100" align="center"></el-table-column>
        <el-table-column label="图片" min-width="100" align="center">
          <template v-slot="scope">
            <el-image
                referrerpolicy="no-referrer"
                style="width: 66px!important; height: 66px!important;"
                :src="imageUrl + scope.row.url"
                fit="scale-down"
                :preview-src-list="[imageUrl + scope.row.url]"
            />
          </template>
        </el-table-column>
        <el-table-column label="排序" prop="sort" min-width="100" align="center"/>
        <el-table-column align="center" label="状态" prop="status" min-width="100">
          <template slot-scope="scope">
            <el-switch
                v-model="scope.row.status"
                :active-value="false"
                :inactive-value="true"
                style="width:40px;"
                @change="handleStatusChange($event,scope.row.id)"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" prop="createTime" min-width="100" align="center"/>
        <el-table-column label="操作" min-width="100" fixed="right" align="center">
          <template slot-scope="scope">
            <el-button icon="el-icon-edit" size="mini" type="primary" @click="handlerOpenEdit(1,scope.row)"
                       v-has-permi="['admin:banner:save']">编辑
            </el-button>
            <el-button icon="el-icon-delete" size="mini" type="danger" @click="handlerDelete(scope.row.id)"
                       v-has-permi="['admin:banner:delete']">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          :current-page="listPram.page"
          :page-sizes="[10,20,40,60,80,100]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="listData.count"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
      />
    </el-card>
    <el-dialog
        :visible.sync="editDialogConfig.visible"
        :title="editDialogConfig.isCreate === 0? '添加':'编辑'"
        destroy-on-close
        :close-on-click-modal="false"
        top="200px"
        width="900px"
    >
      <edit
          :view="editDialogConfig.view"
          v-if="editDialogConfig.visible"
          :is-create="editDialogConfig.isCreate"
          :edit-data="editDialogConfig.editData"
          @hideEditDialog="hideEditDialog"
      />
    </el-dialog>
  </div>
</template>

<script>
import edit from './edit.vue'
import {checkPermi} from "@/utils/permission";
import {list, deleteRow, updateStatus} from "@/api/banner";

export default {
  components: {edit},
  data() {
    return {
      listData: {list: [], count: 0},
      editDialogConfig: {
        visible: false,
        isCreate: 0, // 0=创建，1=编辑
        editData: {},
        view: false,
      },
      imageUrl: '',
      listPram: {
        limit: 20,
        // name: '',
        page: 1
      },
      editDialog: {
        show: false,
        id: null
      },
      tableLoading: false,
    }
  },
  mounted() {
    this.getList()
    this.imageUrl = process.env.VUE_APP_IMAGE_URL
  },
  methods: {
    checkPermi,
    getList() {
      this.tableLoading = true
      list(this.listPram).then(res => {
        this.listData = res;
      }).finally(() => {
        this.tableLoading = false;
      })
    },
    openEdit(id = null) {
      this.editDialog.id = id;
      this.editDialog.show = true
    },

    handlerDelete(id) {
      this.$confirm('确认删除该数据吗？').then(() => {
        const loading = this.$loading({text: '删除中', background: 'rgba(255,255,255,.5)'})
        deleteRow(id).then(() => {
          this.$message.success('删除成功')
          this.getList()
        }).finally(() => {
          loading.close()
        })
      })
    },
    hideEditDialog() {
      this.editDialogConfig.visible = false
      this.editDialogConfig.view = false;
      this.getList()
    },
    handleSizeChange(val) {
      this.listPram.limit = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listPram.page = val
      this.getList()
    },
    handlerOpenEdit(isCreate, editDate) {
      this.editDialogConfig.editData = editDate
      this.editDialogConfig.isCreate = isCreate
      this.editDialogConfig.visible = true
      this.editDialogConfig.view = false;
    },
    handleStatusChange(status, id) {
      const loading = this.$loading({text: '更新中', background: 'rgba(255,255,255,.5)'})
      updateStatus(id, status).then(() => {
        this.$message.success('更新状态成功')
        this.getList()
      }).finally(() => {
        loading.close()
      })
    },
    resetQuery() {
      this.listPram.userType = null;
      this.getList()
    },
  }
}
</script>

<style>

.selWidth .el-input__inner {
  border-radius: 30px; /* 增加圆角 */
}

.custom-dialog .el-dialog__header {
  background-color: #197ceb; /* 设置顶部背景颜色为蓝色 */
  color: white; /* 设置文字颜色为白色 */
  padding: 10px 10px; /* 调整内边距以使宽度变窄 */
  padding-bottom: 5px !important;
  /*width: auto; !* 可选：自动调整宽度 *!*/
  /*max-width: 90%; !* 可选：设置最大宽度 *!*/
}

.custom-dialog .el-dialog__header h2,
.custom-dialog .el-dialog__header .el-dialog__title {
  color: white !important; /* 强制设置标题颜色为白色 */
}

.custom-dialog .el-dialog__header .el-dialog__close {
  color: white !important; /* 强制设置关闭按钮颜色为白色 */
}
</style>
